import CommonHeader from '@/components/common-header/index'
import ChangePassword from '@/apis/ChangePassword'
export default {
  name: 'ChangePassword',
  components: {
    CommonHeader,
  },
  data() {
    return {
      postPassword: '',
      newPassword: '',
      comparePassword: '',
    }
  },
  methods: {
    confirm() {
      if (this.postPassword === '') {
        this.$toast('输入的旧密码不能为空')
        return
      } else if (this.newPassword === '') {
        this.$toast('输入的新密码不能为空')
        return
      } else if (this.comparePassword === '') {
        this.$toast('请再次输入您的新密码')
        return
      } else if (this.newPassword !== this.comparePassword) {
        this.$toast('两次输入的新密码不一致')
        this.newPassword = ''
        this.comparePassword = ''
        return
      }
      ChangePassword(
        {
          jobNumber: this.$store.state.userInfo.jobNumber,
          oldPassword: this.postPassword,
          newPassword: this.newPassword,
        },
        this.$store.state.authorization
      ).then((res) => {
        if (res.data.flag === true) {
          this.$toast('修改密码成功')
          this.postPassword = ''
          this.newPassword = ''
          this.comparePassword = ''
        } else {
          this.$toast('旧密码错误')
          this.postPassword = ''
          this.newPassword = ''
          this.comparePassword = ''
        }
      })
    },
  },
}
